package com.caregiver.watch.business.data.parser;

import com.caregiver.watch.business.data.protobuf.HisDataOuterClass;
import com.caregiver.watch.business.data.protobuf.HisEcgData;
import com.caregiver.watch.business.data.protobuf.HisHealthData;
import com.caregiver.watch.business.data.protobuf.HisRriData;
import com.caregiver.watch.business.data.service.HealthDataService;
import com.caregiver.watch.common.utils.time.DateUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.caregiver.watch.business.data.enums.PayloadOptTypeProtoEnum.HIS_DATA;

/**
 * 0x80协议解析
 */
@AllArgsConstructor
@Component
@Slf4j
public class HistoryDataParser implements IDataParser {


    private final HealthDataService healthDataService;

    @Override
    public Integer getOpt() {
        return HIS_DATA.getOpt();
    }

    @Override
    public void dataParse(Long userId, String deviceId, byte[] payload) {
        HisDataOuterClass.HisNotification hisNotify;
        try {
            hisNotify = HisDataOuterClass.HisNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            log.error(ex.getMessage());
            return;
        }
        if (hisNotify.hasHisData()) {
            HisDataOuterClass.HisData hisData = hisNotify.getHisData();
            // health data
            if (hisData.hasHealth()) {
                // 解析健康数据
                parseHealthData(hisData.getHealth(), userId, deviceId);
            }
            // 心电图数据
            if (hisData.hasEcg()) {
                // 解析ecg数据
                parseEcgData(hisData.getEcg(), userId, deviceId);
            }
            // 房颤数据
            if (hisData.hasRri()) {
                // 解析rri数据
                parseRriData(hisData.getRri(), userId, deviceId);
            }
            // 处理睡眠数据
            parseSleepData(hisData, userId, deviceId);
        }
    }

    /**
     * 处理睡眠数据
     *
     * @param hisData
     */
    private void parseSleepData(HisDataOuterClass.HisData hisData, Long userId, String deviceId) {
        LocalDateTime dataTime;
        if (hisData.hasHealth()) {
            dataTime = DateUtils.convert2LocalDateTime(hisData.getHealth().getTimeStamp().getDateTime().getSeconds());
        } else {
            dataTime = LocalDateTime.now();
        }
        // 睡眠原始数据
        healthDataService.insertSleepData(hisData, deviceId, userId, dataTime);
    }

    /**
     * 解析健康数据
     *
     * @param healthData
     */
    private void parseHealthData(HisHealthData.HisDataHealth healthData, Long userId, String deviceId) {
        LocalDateTime dataTime = DateUtils.convert2LocalDateTime(healthData.getTimeStamp().getDateTime().getSeconds());
        // 保存运动数据
        healthDataService.insertSportData(healthData, deviceId, userId, dataTime);
        // 心率数据
        healthDataService.insertHeartRateData(healthData, deviceId, userId, dataTime);
        // 血氧
        healthDataService.insertBloodOxygen(healthData, deviceId, userId, dataTime);
        // 血压
        healthDataService.insertBpData(healthData, deviceId, userId, dataTime);
        // 疲劳数据
        healthDataService.insertFatigueData(healthData, deviceId, userId, dataTime);
        // 体温数据
        healthDataService.insertTemperatureData(healthData, deviceId, userId, dataTime);
        // 血压心率
        healthDataService.insertBpHeartRateData(healthData, deviceId, userId, dataTime);
    }

    /**
     * 解析ecg数据
     *
     * @param ecgData
     */
    private void parseEcgData(HisEcgData.HisDataECG ecgData, Long userId, String deviceId) {
        LocalDateTime dataTime = DateUtils.convert2LocalDateTime(ecgData.getTimeStamp().getDateTime().getSeconds());
        healthDataService.insertEcgData(ecgData, deviceId, userId, dataTime);
    }


    private void parseRriData(HisRriData.HisDataRRI rri, Long userId, String deviceId) {
        LocalDateTime dataTime = DateUtils.convert2LocalDateTime(rri.getTimeStamp().getDateTime().getSeconds());
        healthDataService.insertRriData(rri, deviceId, userId, dataTime);
    }

}
